package thread;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import entity.User;
import net.sf.json.JSONArray;
import ui.P2PFrame;
import ui.P2PFrame2;
import ui.QQFrame;

public class QunFa implements Runnable{

	private Socket socket;
	private JTextArea ta;
	private JPanel sidePanel;
	private QQFrame frame;
	private String myusername;
	private JScrollPane jsp;
	public static Map<String, P2PFrame2> framemap2 = new HashMap<String, P2PFrame2>();// 全局
	public static Map<String, P2PFrame> framemap1 = new HashMap<String, P2PFrame>();// 全局
	
	public QunFa(Socket socket, JTextArea ta,JPanel sidePanel,QQFrame frame,JScrollPane jsp,String myusername) {
		super();
		this.myusername = myusername;
		this.socket = socket;
		this.ta = ta;
		this.sidePanel=sidePanel;
		this.frame=frame;
		this.jsp =jsp;
	}


	@Override
	public void run() {
		// TODO Auto-generated method stub
		while(true){
		try {
			
			BufferedReader reader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
			String ml=reader.readLine();
			System.out.println("client:ml "+ml);
			if("qfandzf".equals(ml)){
		    String msg=reader.readLine();
			pri(msg);
			}
			if("haoyoulibiao".equals(ml)){
				String json=reader.readLine();
//				System.out.println("client"+json);
				JSONArray ja = JSONArray.fromObject(json);
				List<Map<String,User>> list = JSONArray.toList(ja, HashMap.class);
				haoyoulibiao(list);
			}if("dakaidl".equals(ml)){
				String hisusername=reader.readLine();
				try {
					P2PFrame2 p2p=new P2PFrame2(hisusername,myusername,socket);
					framemap2.put(hisusername, p2p);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}if("danliao".equals(ml)){
				String hisusername=reader.readLine();
				String msg=reader.readLine();
				Set<String> set = framemap2.keySet();
				String username = null;
				for (String str : set) {
					username = str;
				}
				if (username.equals(hisusername)) {
					P2PFrame2 frame = framemap2.get(username);
					frame.getTa().append(msg+"\n");
				}
			}if("exdanliao".equals(ml)){
				String hisusername=reader.readLine();
				String msg=reader.readLine();
				Set<String> set = framemap1.keySet();
				String username = null;
				for (String str : set) {
					username = str;
				}
				if (username.equals(hisusername)) {
					P2PFrame frame = framemap1.get(username);
					frame.getTa().append(msg+"\n");
				}
			}if("dfzd".equals(ml)){
				String hisusername=reader.readLine();
				Set<String> set = framemap2.keySet();
				String username = null;
				for (String str : set) {
					username = str;
				}
				if (username.equals(hisusername)) {
					P2PFrame2 frame = framemap2.get(username);
					frame.doudong();
				}
			}if("exdfzd".equals(ml)){
				String hisusername=reader.readLine();
				Set<String> set = framemap1.keySet();
				String username = null;
				for (String str : set) {
					username = str;
				}
				if (username.equals(hisusername)) {
					P2PFrame frame = framemap1.get(username);
					frame.doudong();
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		}
	
	}
	public void pri(String msg){
	//	System.out.println("client"+msg);
		ta.append(msg+"\n");
	}
	public void haoyoulibiao(List<Map<String,User>> list){
		sidePanel=new JPanel(new FlowLayout());
		sidePanel.setPreferredSize(new Dimension(100, 300));//���ô�С�����ο�
		for (Map<String, User> map : list) {
			Set<String> set = map.keySet();
			String username=null;
			for (String string : set) {
				username=string;
				
			}
			JButton btn=new JButton(username);
			sidePanel.add(btn);
		}
		jsp=new JScrollPane(sidePanel);
		frame.add(jsp, BorderLayout.EAST);
		frame.setVisible(true);//��Ⱦ
	}
}
